package com.example.mvc.controller;

import com.example.mvc.bean.Book;
import com.example.mvc.service.BookService;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

@WebServlet("/addBookServlet")
public class AddBookServlet extends HttpServlet {
 BookService bookService = new BookService();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String card = req.getParameter("card");
        String name = req.getParameter("name");
        String type = req.getParameter("type");
        String autho = req.getParameter("autho");
        String press = req.getParameter("press");
        String num = req.getParameter("num");
        Book book = new Book();
        book.setCard(card);
        book.setName(name);
        book.setType(type);
        book.setAutho(autho);
        book.setPress(press);
        book.setNum(Integer.parseInt(num));
        int count = bookService.addBook(book);
        String contextPath = req.getContextPath();
        resp.sendRedirect(contextPath+"/bookListServlet");


    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }
}
